﻿
// 活动工厂
var XWFActivityFactory = {
    _nameArray: new Array(),
    _handlerArray: new Array(),
    // 注册活动的创建方法
    register: function (name, handler) {
        for (var index = 0; index < this._nameArray.length; index++) {
            if (this._nameArray[index] == name) {
                return;
            }
        }

        this._nameArray.push(name);
        this._handlerArray.push(handler);
    },
    // 创建活动实例
    create: function (name) {
        for (var index = 0; index < this._nameArray.length; index++) {
            if (this._nameArray[index] == name) {
                return this._handlerArray[index]();
            }
        }
        return null;
    }
};

// 视图父类
function XWFView() {
    //DOM元素
    this._domEl = null;
    this._position = null;
    this._pos = { x: 0, y: 0 };
    this._size = { w: 0, h: 0 };
    //是否已经安装完毕
    this._isInstalled = false;

    //设置视图位置
    //@x:double,x坐标
    //@y:double,y坐标
    this.setPos = function (x, y) {
        this._pos.x = x;
        this._pos.y = y;

        if (!isNaN(this._pos.x)) {
            this._domEl.style.left = x + 'px';
        } else {
            this._domEl.style.left = x;
        }
        if (!isNaN(this._pos.y)) {
            this._domEl.style.top = y + 'px';
        } else {
            this._domEl.style.top = y;
        }
    }

    //设置视图大小
    //@w:double,宽度
    //@h:double,高度
    this.setSize = function (w, h) {
        this._size.w = w;
        this._size.h = h;
        if (!isNaN(this._size.w)) {
            this._domEl.style.width = w + 'px';
        } else {
            this._domEl.style.width = w;
        }
        if (!isNaN(this._size.h)) {
            this._domEl.style.height = h + 'px';
        } else {
            this._domEl.style.height = h;
        }
    }

    // 更新图形，根据所在容器的位置和大小来更新图形的位置和大小
    // @containerPos:Point容器的位置
    // @containerSize:Size容器的大小
    this.update = function (containerPos, containerSize) {
        var x, y, w, h;
        if (this._position.w == null) {
            // 宽度需要自动计算
            if (this._position.l != null) {
                if (this._position.r != null) {
                    w = containerSize.w - this._position.l - this._position.r;
                } else {
                    w = containerSize.w - this._position.l;
                }
            } else {
                if (this._position.r != null) {
                    w = containerSize.w - this._position.r;
                } else {
                    w = containerSize.w;
                }
            }
        } else {
            w = this._position.w;
        }

        if (this._position.h == null) {
            // 自动计算高度
            if (this._position.t != null) {
                if (this._position.b != null) {
                    h = containerSize.h - this._position.t - this._position.b;
                } else {
                    h = containerSize.h - this._position.t;
                }
            } else {
                if (this._position.b != null) {
                    h = containerSize.h - this._position.b;
                } else {
                    h = containerSize.h;
                }
            }
        } else {
            h = this._position.h;
        }

        if (this._position.l != null) {
            x = containerPos.x + this._position.l;
        } else {
            if (this._position.r != null) {
                x = containerPos.x + (containerSize.w - w - this._position.r);
            } else {
                x = containerPos.x + (containerSize.w - w) / 2;
            }
        }

        if (this._position.t != null) {
            y = containerPos.y + this._position.t;
        }
        else {
            if (this._position.b != null) {
                y = containerPos.y + (containerSize.h - h - this._position.b);
            } else {
                y = containerPos.y + (containerSize.h - h) / 2;
            }
        }

        this.setPos(x, y);
        this.setSize(w, h);
    }

    //获取DOM元素
    this.getDomEl = function () {
        return this._domEl;
    }

    //安装和初始化视图
    //@designer:XDesigner,设计器
    //@callback:Function,视图安装和初始化完毕之后的回调方法
    this.install = function (designer, callback) {
        if (this._domEl == null) {
            this._domEl = document.createElement("div");
            this._domEl.style.position = "absolute";
            designer.getDomEl().appendChild(this._domEl);

            this.onInstall(designer, callback);
        }
    }

    //安装和初始化视图
    //#abstract
    //@designer:XDesigner,设计器
    //@callback:Function,视图安装和初始化完毕之后的回调方法
    this.onInstall = function (designer, callback) {

    }

    var that = this;
    //加载模版
    //@url: 模版path
    //@callback: 加载模版后的callback, 模版里的事件可写在callback里
    this.includeHtml = function (url, callback) {
        $.ajax({
            type: 'get',
            url: url,
            data: [],
            dataType: "text",
            success: function (data) {
                $(that._domEl).append($(data));
                if (callback) {
                    callback();
                }
            }
        });
    }
}

// 设计器
function XWFDesigner() {
    this._views = new Array();

    // 工具视图
    this._toolboxView = null;
    // 渲染视图
    this._renderView = null;

    this._callback = null;

    // 初始化设计器
    // @domEl:object,设计器所在的目标DOM元素
    // @callback:function,设计器初始化完毕之后的回调方法
    this.init = function (domEl, callback) {
        this._domEl = domEl;

        this._callback = callback;

        // 注册事件
        var that = this;
        var oldFunc = window.onmousedown;
        if (oldFunc != null) {
            window.onmousedown = function (e) {
                oldFunc(e);
                that.onMouseDown(e);
            }
        } else {
            window.onmousedown = function (e) {
                that.onMouseDown(e);
            }
        }
        window.onmousemove = function (e) {
            that.onMouseMove(e);
        };
        window.onmouseup = function (e) {
            that.onMouseUp(e);
        };
        window.onresize = function () {
            that.onSizeChanged();
        };

        // 创建渲染视图
        this._renderView = this.addView("15%", 50, null, null, "69%", null, new XWFRenderView());

        // 创建工具视图
        this._toolboxView = this.addView(0, null, null, null, "15%", null, new XWFToolboxView());

        //创建toolbar视图
        this._toolbarView = this.addView("15%", 0, null, null, "69%", 50, new XWFToolbarView());

        //创建status视图
        this._statusView = this.addView("84%",0, 200, null, "15%", "50%", new XWFStatusView());

        //创建属性试图
        this._propertiesView = this.addView("84%", "50%", null, null, "15%", "49%", new XWFPropertiesView());

        //装载所有的视图
        this.installViews();
    };

    this.installViews = function () {
        var that = this;
        for (var index = 0; index < this._views.length; index++) {
            var view = this._views[index];
            view.install(this, function () {
                view._isInstalled = true;

                var isAllFinished = true;

                for (var index = 0; index < that._views.length; index++) {
                    if (!that._views[index]._isInstalled) {
                        isAllFinished = false;
                        break;
                    }
                }
                if (isAllFinished) {
                    that.inited();
                }
            });
        }
    }

    this.inited = function () {
        this.updateLayout();

        this._callback();
    }

    //添加视图
    //@l:double
    //@t:double
    //@r:double
    //@b:double
    //@w:double
    //@h:double
    //@view:XWFView
    this.addView = function (l, t, r, b, w, h, view) {
        this._views.push(view);
        view._position = new XPosition(l, t, r, b, w, h);
        return view;
    }

    // 更新布局信息
    this.updateLayout = function () {
        var size = {
            w: document.documentElement.clientWidth,
            h: document.documentElement.clientHeight
        };
        var pos = { x: 0, y: 0 };
        for (var index = 0; index < this._views.length; index++) {
            this._views[index].update(pos, size);
        }
    }

    // 新建一个图表
    this.new = function () {
        this._renderView.new();
    }

    // 注册事件
    //@eventName:string
    //@handler:Function
    this.event = function (eventName, handler) {
        this._renderView.event(eventName, handler);
    }

    // 添加活动到流程图中
    this.addActivity = function (x, y, w, h, activity) {
        this._renderView.addActivity(x, y, w, h, activity);
    }

    // 开始放置对象
    this.drawActivity = function (activityClass) {
        this._renderView.drawActivity(activityClass);
    };

    // 开始绘制动作
    this.drawAction = function (actionClass) {
        this._renderView.drawAction(actionClass);
    };

    this.onMouseDown = function (point) {
        // 验证鼠标点是否在renderView里
        this._renderView.onMouseDown(point);
    };
    this.onMouseMove = function (point) {
        // 验证鼠标点是否在renderView里
        this._renderView.onMouseMove(point);
    };
    this.onMouseUp = function (point) {
        // 验证鼠标点是否在renderView里
        this._renderView.onMouseUp(point);
    };
    this.onSizeChanged = function () {
        this.updateLayout();
        //this._renderView.setPos();
        //this._renderView.setSize();
        //this._toolboxView.setPos();
        //this._toolboxView.setSize();
    };

    // 获取设计器DOM元素
    this.getDomEl = function () {
        return this._domEl;
    }
}